package com.yao.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yao.entity.Bloger;
import com.yao.service.BlogerService;
import com.yao.utils.ControllerUtils;
import com.yao.utils.JwtUtils;

@Controller
@RequestMapping("/yao/Bloger")
public class BlogerController {
	@Autowired
	private BlogerService blogerService;

	@ResponseBody
	@RequestMapping("/loginBlogerByNameAndPassword")
	public String loginBlogerByNameAndPassword(HttpServletResponse response, HttpServletRequest request, String account,
			String password) throws IOException {
		Bloger bloger = blogerService.selectBlogerByAccountAndPassword(account, password);
		if (bloger == null)
			return "failed";
		response.setHeader("Access-Control-Allow-Origin", "*");
		String blogerToken = JwtUtils.getToken(bloger, 1000*60*30);
		ControllerUtils.setCookie(response, "bloger", blogerToken);
		return "success";
	}

	@ResponseBody
	@RequestMapping("/loginoutBloger")
	public String loginoutBloger(HttpServletResponse response, HttpServletRequest request, String account,
			String password) throws IOException {
		System.out.println("被访问了");
		request.getSession().removeAttribute("bloger");
		return "success";
	}
}